package snail.validators;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import snail.model.Article;


public class createArticleValidator implements Validator {

    public boolean supports(Class aClass) {
        return Article.class.equals(aClass);
    }

    public void validate(Object obj, Errors errors) {
        Article article = (Article) obj;
        
        //fields cannot be empty
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "buyNowPrice", "field.required", "Required field");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description", "field.required", "Required field");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", "field.required", "Required field");

        if (article.getStartPrice() < 0 || (article.getStartPrice() + "").length() < 1) {
            errors.rejectValue("startPrice", "under.zero.value", "Price too low!");
        }


    }
}
